package com.qianfeng.zhangwei.fruitday.fragment;

import android.content.Intent;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qianfeng.zhangwei.fruitday.R;
import com.qianfeng.zhangwei.fruitday.adapter.GoodTypeGvAdapter;
import com.qianfeng.zhangwei.fruitday.bean.Common;
import com.qianfeng.zhangwei.fruitday.bean.GoodType;
import com.qianfeng.zhangwei.fruitday.bean.SubLevelEntity;
import com.qianfeng.zhangwei.fruitday.config.AppConfig;
import com.qianfeng.zhangwei.fruitday.ui.GoodDetailsActivity;

import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

@EFragment(R.layout.fragment_good_type)
public class GoodTypeFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener {
    @ViewById(R.id.gt_rgs)
    RadioGroup rgs;
    List<Common> commons;
    /**
     * 分类
     */
    @ViewById(R.id.gt_gridview)
    GridView goodTypeGridview;
    private GoodTypeGvAdapter gvAdapter;
    private List<SubLevelEntity> subLevelList = new ArrayList<>();
    private HttpHandler<String> handler;
    @ViewById(R.id.gt_title)
    TextView titleTv;


    /**
     * 初始化ui
     */
    @Override
    public void initView() {
        rgs.setOnCheckedChangeListener(this);
        gvAdapter = new GoodTypeGvAdapter(subLevelList, activity);
        goodTypeGridview.setAdapter(gvAdapter);
    }

    @Override
    public void initData() {
        handler = httpUtils.send(HttpRequest.HttpMethod.POST, AppConfig.REQUEST_URL, getRequestParams(), new RequestCallBack<String>() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                GoodType goodType = JSONObject.parseObject(responseInfo.result, GoodType.class);
                if (goodType != null) {
                    commons = goodType.getCommon();
                    addRadioButtons();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
            }

            @Override
            public void onCancelled() {
                super.onCancelled();
            }
        });
    }

    public RequestParams getRequestParams() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("sign", "c316fe3aee275e34712eea2cdaa92d70");
        params.addBodyParameter("timestamp", "1438873421");
        params.addBodyParameter("platform", "ANDROID");
        params.addBodyParameter("source", "app");
        params.addBodyParameter("connect_id", "");
        params.addBodyParameter("region_id", "143949");
        params.addBodyParameter("service", "product.getCatList");
        params.addBodyParameter("device_id", "93789e8978f2eefdcf52006b4d96ee23");
        params.addBodyParameter("channel", "qq");
        params.addBodyParameter("version", "2.3.0");
        return params;
    }

    @Override
    public void onStop() {
        super.onStop();
        handler.cancel();
    }

    @ItemClick(R.id.gt_gridview)
    public void onItemClickListener(int position) {
        Intent intent = new Intent(activity, GoodDetailsActivity.class);
    }

    private void addRadioButtons() {
        for (int i = 0; i < commons.size(); i++) {
            RadioButton rb = new RadioButton(activity);
            rb.setId(0x7f31004 + i);
            RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            rb.setLayoutParams(params);
            rb.setButtonDrawable(android.R.color.transparent);
            rb.setText(commons.get(i).getName());
            rb.setPadding(15, 30, 15, 30);
            rb.setTextSize(16);
            rgs.addView(rb);
            if (i == 0) {
                rb.setBackgroundColor(getResources().getColor(R.color.white));
                rb.setTextColor(getResources().getColor(R.color.green));
                rb.setChecked(true);
            } else {
                rb.setBackgroundColor(getResources().getColor(R.color.transparent));
                rb.setTextColor(getResources().getColor(R.color.gray));
            }

        }
    }

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int i = 0; i < group.getChildCount(); i++) {
            if (group.getChildAt(i).getId() == checkedId) {
                ((RadioButton) group.getChildAt(i)).setTextColor(getResources().getColor(R.color.green));
                group.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.white));
                titleTv.setText(commons.get(i).getName());
                subLevelList.clear();
                subLevelList.addAll(commons.get(i).getSub_level());
                gvAdapter.notifyDataSetChanged();
            } else {
                group.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.transparent));
                ((RadioButton) group.getChildAt(i)).setTextColor(getResources().getColor(R.color.gray));
            }
        }
    }

}
